package com.yuandian.management.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yuandian.api.customer_perception.po.SystemOverviewParams;
import com.yuandian.api.management.validate.InsertFlag;
import com.yuandian.api.management.vo.AreaDataVo;
import com.yuandian.bpm.common.core.util.R;
import com.yuandian.management.service.ConfAreaService;
import com.yuandian.management.vo.RegionMappingExcelVo;
import com.yuandian.management.vo.RegionMappingInfo;
import com.yuandian.management.vo.RegionMappingVo;
import com.yuandian.management.vo.SearchOptionVo;
import com.yuandian.utils.poi.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotNull;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 营业厅配置表-Controller
 * @author zfsn
 * @date 2023-3-28
 */
@Api(tags = "外部指标管理-导入导出管理-区域编码")
@RestController
@RequestMapping("/area")
public class ConfAreaController {

	@Autowired
	private ConfAreaService confAreaService;

	/**
	 * 通过主键查询数据
	 * @param idList 主键集合
	 * @return 通用返回对象
	 */
	@ApiOperation(value = "通过主键查询数据", notes = "通过主键查询数据，支持批量查询")
	@GetMapping("/{id}")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "ids", value = "id(支持多选)", paramType = "query", required = true, dataType = "Integer"),
	})
	public R<List<RegionMappingInfo>> queryDataByIds(@PathVariable("id") Integer idList) {
		return R.ok(confAreaService.queryDataByIds(idList));
	}

	/**
	 * 分页查询
	 * @param regionMappingVo 筛选条件
	 * @return 通用返回对象
	 */
	@ApiOperation(value = "分页查询", notes = "分页查询")
	@PostMapping
	@ApiImplicitParams({
			@ApiImplicitParam(name = "code", value = "区域行政编码", paramType = "body", required = false, dataType = "String"),
			@ApiImplicitParam(name = "name", value = "区域名称", paramType = "body", required = false, dataType = "String"),
			@ApiImplicitParam(name = "pageSize", value = "每页大小", paramType = "body", required = true, dataType = "Integer"),
			@ApiImplicitParam(name = "pageNum", value = "当前页数", paramType = "body", required = true, dataType = "Integer"),
			@ApiImplicitParam(name = "orderByColumn", value = "排序列", paramType = "body", required = true, dataType = "String"),
			@ApiImplicitParam(name = "isAsc", value = "排序的方向值只能是 desc 或者 asc", paramType = "body", required = true, dataType = "String")
	})
	public R<Page<RegionMappingVo>> page(@RequestBody @ApiIgnore RegionMappingVo regionMappingVo) {
		return R.ok(confAreaService.listPage(regionMappingVo));
	}

	/**
	 * 新增数据
	 * @param regionMappingInfo 实例对象
	 * @return 通用返回对象
	 */
	@ApiOperation(value = "新增数据", notes = "新增数据")
	@PostMapping("/save")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "code", value = "区域行政编码", paramType = "body", required = true, dataType = "String"),
			@ApiImplicitParam(name = "areaId", value = "区域id", paramType = "body", required = true, dataTypeClass = Integer.class),
			@ApiImplicitParam(name = "parentId", value = "省级id", paramType = "body", required = true, dataTypeClass = Integer.class),
	})
	public R<String> save(@ApiIgnore @RequestBody @Validated(InsertFlag.class) RegionMappingInfo regionMappingInfo) {
		return confAreaService.saveConfArea(regionMappingInfo);
	}

	/**
	 * 通过主键更新数据
	 * @param regionMappingInfo 实例对象
	 * @return 通用返回对象
	 */
	@ApiOperation(value = "通过主键更新数据", notes = "通过主键更新数据")
	@PutMapping
	@ApiImplicitParams({
			@ApiImplicitParam(name = "id", value = "id", paramType = "body", required = true, dataTypeClass = Integer.class),
			@ApiImplicitParam(name = "code", value = "区域行政编码", paramType = "body", required = true, dataType = "String"),
			@ApiImplicitParam(name = "areaId", value = "区域id", paramType = "body", required = true, dataTypeClass = Integer.class),
			@ApiImplicitParam(name = "parentId", value = "省级id", paramType = "body", required = true, dataTypeClass = Integer.class),
	})
	public R<String> updateById(@ApiIgnore @RequestBody RegionMappingInfo regionMappingInfo) {
		return confAreaService.updateConfAreaById(regionMappingInfo);
	}

	/**
	 * 通过主键删除数据
	 * @param ids 主键
	 * @return 通用返回对象
	 */
	@ApiOperation(value = "通过主键删除数据", notes = "通过主键删除数据")
	@DeleteMapping("/delete/{ids}")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "ids", value = "id(支持多选)", paramType = "query", required = true, dataType = "Integer")
	})
	public R<String> removeByIds(@PathVariable("ids") List<Integer>  ids){
		return confAreaService.removeDataByIds(ids);
	}

	/**
	 * 获取省份信息
	 * @return 通用返回对象
	 */
	@ApiOperation(value = "获取省份信息", notes = "获取省份信息")
	@GetMapping
	public R<List<Map<String, String>>> getProvince() {
		return confAreaService.getProvince();
	}

	/**
	 * 通过code获取区域信息
	 * @return 通用返回对象
	 */
	@ApiIgnore
	@GetMapping("/administrativeCode/{code}")
	public R<List<Map<String, String>>> getCity(@NotNull @PathVariable("code") String code) {
		return confAreaService.getCity(code);
	}

	/**
	 * 获取所有区域信息
	 * @return 通用返回对象
	 */
	@ApiIgnore
	@GetMapping("/getAreaCodeAndName")
	public R<List<Map<String, String>>> getAreaCodeAndName() {
		return R.ok(confAreaService.getAreaCodeAndName());
	}

	/**
	 * 根据区域拼音查询子集信息
	 * @return 通用返回对象
	 */
	@ApiIgnore
	@GetMapping("/queryCodeByParentName/{areaName}")
	public R<List<AreaDataVo>> queryCodeByParentName(@PathVariable("areaName") String areaName) {
		return R.ok(confAreaService.queryCodeByParentName(areaName));
	}

	/**
	 * 区域编码模板下载
	 *
	 * @param response response
	 * @throws IOException
	 */
	@PostMapping("/downloadTemplate")
	@ApiOperation(value = "区域编码模板下载", notes = "区域编码模板下载")
	public void getTemplate(HttpServletResponse response) throws IOException {
		ExcelUtil.writeExcel(RegionMappingExcelVo.class, "区域编码信息模板", "区域编码信息模板", new ArrayList<>(), response);
	}

	/**
	 * 区域编码信息上传
	 *
	 * @param file file
	 * @return Result
	 * @throws IOException
	 */
	@PostMapping("/upload")
	@ApiOperation(value = "区域编码信息上传", notes = "区域编码信息上传")
	public R upload(@RequestParam("file") MultipartFile file) throws IOException {
		Map<String, List<RegionMappingExcelVo>> resultMap = ExcelUtil.readAndSaveExcelData(file, 0, 2, RegionMappingExcelVo.class, confAreaService);

		List<RegionMappingExcelVo> successList = resultMap.get("result");
		List<RegionMappingExcelVo> failList = resultMap.get("fail");
		if (!CollectionUtils.isEmpty(successList) && CollectionUtils.isEmpty(failList)) {
			return R.ok("导入完成，成功条数：" + successList.size() + ",失败条数：" + failList.size());
		}

		return R.failed("导入完成，成功条数：" + successList.size() + ",失败条数：" + failList.size());
	}

	/**
	 * 根据code获取区域名称
	 * @param areaCode
	 * @return
	 */
	@GetMapping("/queryAreaName")
	public R<String> queryAreaName(@RequestParam("areaCode") String areaCode){
		return confAreaService.queryAreaName(areaCode);
	}

	/**
	 * 查询地市id和名称
	 * @param params 请求参数
	 * @return 查询地市id和名称
	 */
	@PostMapping("/queryAreaCodeAndName")
	public R<List<SearchOptionVo>> queryAreaCodeAndName(@RequestBody SystemOverviewParams params) {
		return R.ok(this.confAreaService.queryAreaCodeAndName(params));
	}
}
